#include "pch.h"
#include "seLifeBar.h"

seLifeBar::seLifeBar() : mMaxValue(1000.0f)
{
}

seLifeBar::~seLifeBar()
{
}

void seLifeBar::Load()
{
		
	mBubbleImage.LoadFromFile("data/bubble.png");
	mBarFill.LoadFromFile("data/lifebar-fill.png");
	mBarCover.LoadFromFile("data/lifebar-cover.png");

	mBarFillSprite.SetImage(mBarFill);
	mBarCoverSprite.SetImage(mBarCover);
}

void seLifeBar::SetDecreaseSpeed(float decreaseSpeed)
{
	mDecreaseSpeed = decreaseSpeed;
}

void seLifeBar::SetDisplayWidth(float displayWidth)
{
	mDisplayWidth = displayWidth;
}

	
void seLifeBar::SetCurrentValue(float currentValue)
{
	mCurrentValue=currentValue;
}

void seLifeBar::Init()
{
	mCurrentValue=mMaxValue;
	mDecreaseSpeed=0;
	mDisplayWidth=200;

	fCounter = 0.0f;



	mBarFillSprite.SetPosition( 10*viewWidth/1920 , viewHeight - 10*viewWidth/1920 - mBarFill.GetHeight()*viewWidth/1920);
	mBarCoverSprite.SetPosition( 10*viewWidth/1920 ,  viewHeight - 10*viewWidth/1920 - mBarFill.GetHeight()*viewWidth/1920);

	mBarFillSprite.SetScale(viewWidth/1920, viewWidth/1920);
	mBarCoverSprite.SetScale(viewWidth/1920, viewWidth/1920);
}
void seLifeBar::Step()
{	
	fCounter += ONE_FRAME_DURATION;
	if (fCounter > 0.1f && mCurrentValue != 0) // jezeli czas na nowa czastke
	{

		mBubbles.push_back(sf::Sprite(mBubbleImage));
		if(mCurrentValue > 0&& mBarFill.GetWidth()>0)
			mBubbles.back().SetPosition( 10*viewWidth/1920 + rand() % ((int)((mCurrentValue/mMaxValue)*(mBarFill.GetWidth()*viewWidth/1920) )+1),  viewHeight - 10*viewWidth/1920 - mBubbleImage.GetHeight() );
		mBubbles.back().SetCenter(mBubbles.back().GetSize().x / 2,mBubbles.back().GetSize().y / 2);
		mBubbles.back().SetScale(0.4f, 0.4f);
		//mBubbles.back().SetScale(sf::Vector2f(mScale,mScale));
		fCounter = 0.0f;

	}

	for (unsigned int i=0;i<mBubbles.size();++i)
	{
		mBubbles[i].SetPosition(mBubbles[i].GetPosition().x,mBubbles[i].GetPosition().y-0.5);
		mBubbles[i].SetScale(mBubbles[i].GetScale().x*1.01f, mBubbles[i].GetScale().y*1.01f);
		mBubbles[i].SetColor(sf::Color(255,255,255, (1.2 - mBubbles[i].GetScale().x)*255 +50));

	}
	
	for (unsigned int i=0;i<mBubbles.size();++i)
		if (mBubbles[i].GetPosition().y <  viewHeight - 10*viewWidth/1920 - mBarFill.GetHeight()*viewWidth/1920 + mBubbleImage.GetHeight() || mBubbles[i].GetPosition().x + mBubbleImage.GetWidth() > mBarFillSprite.GetPosition().x + (mCurrentValue/mMaxValue)*(mBarFill.GetWidth()*viewWidth/1920))
		{
			mBubbles.erase(mBubbles.begin() + i);
			i--;
		}



	mCurrentValue -= mDecreaseSpeed/60.0f;
	if (mCurrentValue<0) mCurrentValue = 0;

	mBarFillSprite.SetScale(viewWidth/1920, viewWidth/1920);
	mBarCoverSprite.SetScale(viewWidth/1920, viewWidth/1920);

}
void seLifeBar::Draw()
{	
	mDisplayWidth=viewWidth/2-100;


	//app.Draw( sf::Shape::Rectangle( viewWidth/2-mDisplayWidth/2, 20, viewWidth/2+mDisplayWidth/2, 50, sf::Color::Black,5,sf::Color::White));
	//app.Draw( sf::Shape::Rectangle( viewWidth/2-mDisplayWidth/2, 20,viewWidth/2-mDisplayWidth/2+mDisplayWidth*(mCurrentValue/mMaxValue), 50, sf::Color::Red));

	mBarFillSprite.SetSubRect(sf::IntRect(0,0, (mCurrentValue/mMaxValue)*mBarFill.GetWidth(), mBarFill.GetHeight() ));

	
	mBarFillSprite.SetPosition( 10*viewWidth/1920 , viewHeight - 10*viewWidth/1920 - mBarFill.GetHeight()*viewWidth/1920);
	mBarCoverSprite.SetPosition( 10*viewWidth/1920 ,  viewHeight - 10*viewWidth/1920 - mBarFill.GetHeight()*viewWidth/1920);

	/*mBarFillSprite.SetPosition( viewWidth/2-mBarFill.GetWidth()/2 , viewHeight - 20 - mBarFill.GetHeight());
	mBarCoverSprite.SetPosition( viewWidth/2-mBarFill.GetWidth()/2 ,  viewHeight - 20 - mBarFill.GetHeight());*/

	app.Draw(mBarCoverSprite);
	app.Draw(mBarFillSprite);


	for(unsigned int i = 0; i < mBubbles.size(); ++i)
	{
		app.Draw(mBubbles[i]);
	}

}

void seLifeBar::IncreaseDecreaseSpeed(float delta)
{
	mDecreaseSpeed += delta;
}

void seLifeBar::DesreseSpeedOfBliding(float delta)
{
	mDecreaseSpeed -= delta;
	mDecreaseSpeed = (mDecreaseSpeed < 0 ? 0 : mDecreaseSpeed);
}

float seLifeBar::GetCurrentValue()
{
	return mCurrentValue;
}

float seLifeBar::GetMaxValue()
{
	return mMaxValue;
}